package com.lx.filter;

import com.lx.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName: AccessControllerFilter
 * @Description:
 * @Date Created in  2021-07-13 3:32
 * @Modified By:
 * @Author: 零巽
 */
@WebFilter({"/admin/index.html","/admin/views/*","/admin/express/*","/admin/user/*","/admin/courier/*"})
public class AccessControllerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpSession session = req.getSession();
        String userName = UserUtil.getAdminUserName(session);
        if (userName != null && userName.length() > 0) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            HttpServletResponse resp = (HttpServletResponse) servletResponse;
            resp.sendError(404,"权限不足，拒绝访问，请登录后再试！");
        }

    }

    @Override
    public void destroy() {

    }
}
